package com.king.common.exception;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.util.CollectionUtils;

/**
 * Description: 断言辅助类
 *
 * @author Xander
 * datetime: 2022-06-06 14:46
 */
public class AssertHelper {

    private volatile static AssertHelper instance = null;

    private AssertHelper() {
    }

    public static AssertHelper getInstance() {
        if (instance == null) {
            synchronized (AssertHelper.class) {
                if (instance == null) {
                    instance = new AssertHelper();
                }
            }
        }
        return instance;
    }

    /**
     * 检查，如果 throwException
     *
     * @param throwException
     * @param exceptionMsg
     */
    public void checkException(boolean throwException, String exceptionMsg) {
        if (throwException) {
            throw new ServiceException(exceptionMsg);
        }
    }

    /**
     * 判断是否为空
     * @param obj
     * @param exceptionMsg
     */
    public void checkNullException(Object obj, String exceptionMsg) {
        if (obj == null) {
            throw new ServiceException(exceptionMsg);
        }
    }
    
    /**
     * 判断字符串是否为空
     * @param obj
     * @param exceptionMsg
     */
    public void checkEmptyException(String obj, String exceptionMsg) {
        if (StringUtils.isEmpty(obj)) {
            throw new ServiceException(exceptionMsg);
        }
    }
    
    /**
     * 判断集合是否为空
     * @param obj
     * @param exceptionMsg
     */
    public void checkEmptyException( Collection<?> coll, String exceptionMsg) {
        if (CollectionUtils.isEmpty(coll)) {
            throw new ServiceException(exceptionMsg);
        }
    }
    
    /**
     * 判断集合部位为空
     * @param coll
     * @param exceptionMsg
     */
    public void checkNotEmptyException( Collection<?> coll, String exceptionMsg) {
        if (!CollectionUtils.isEmpty(coll)) {
            throw new ServiceException(exceptionMsg);
        }
    }

    /**
     * 直接抛出异常信息
     * @param exceptionMsg
     */
	public void throwException(String exceptionMsg) {
		throw new ServiceException(exceptionMsg);
	}
	
	
	/**
	 * 抛出异常信息和异常代码
	 * @param exceptionMsg
	 * @param code
	 */
	public void throwException(String exceptionMsg, int code) {
		throw new ServiceException(exceptionMsg, code);
	}
	
	
	/**
	 * 获取exception的详细错误信息。
	 */
	public String getExceptionMessage(Throwable e) {
		StringWriter sw = new StringWriter();
		e.printStackTrace(new PrintWriter(sw, true));
		return sw.toString();
	}

	/**
	 * 获取异常详细信息
	 * @param e
	 * @return
	 */
	public String getRootErrorMessage(Exception e) {
		Throwable root = ExceptionUtils.getRootCause(e);
		root = (root == null ? e : root);
		if (root == null) {
			return "";
		}
		String msg = root.getMessage();
		if (msg == null) {
			return "null";
		}
		return StringUtils.defaultString(msg);
	}
}
